package com;

import io.netty.util.internal.StringUtil;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo {
    @Test
    public void test13() {
        AtomicInteger integer = new AtomicInteger(0);
        System.out.println(integer.incrementAndGet());
        System.out.println(integer.incrementAndGet());
    }
    private static final ConcurrentHashMap map = new ConcurrentHashMap();
    @Test
    public void test12() {
//        map.put("11", "123");
    }
    @Test
    public void test10() throws InterruptedException, ExecutionException {
//        map.put("11", "123");
        map.put("11", "123312");
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<?> f = executorService.submit(() -> {
//            System.out.println("sfaddfassdfa");
            map.put("11", "sdfasfad");
        });
        f.get();
//        Thread t = new Thread(() -> map.put("122", "sdfasfad"));
        System.out.println(map);
//        Thread.sleep(1000);
//        System.out.println(map);
    }
    @Test
    public void test9() {
//        FutureTask<List<Integer>> futureTask = new FutureTask<List<Integer>>();
        System.out.println(Runtime.getRuntime().maxMemory());

    }
    @Test
    public void test8() throws ExecutionException, InterruptedException {
        Callable<String> task = () ->{
            return new Date().toString();
        };
        FutureTask<String> f = new FutureTask<>(task);
//        f.run();
        System.out.println(f.get());

    }
    private final ConcurrentMap<Object, Future<String>> taskCache = new ConcurrentHashMap<>();

    private String executionTask(final String taskName)throws ExecutionException, InterruptedException {
        while (true) {
            System.out.println("fdsa");
            Future<String> future = taskCache.get(taskName); // 1.1,2.1
            if (future == null) {
//                Callable<String> task = () -> taskName;
                Callable<String> task = () -> {
                    Thread.sleep(1000);
                    return "sdafadfsadfs";
                };
                FutureTask<String> futureTask = new FutureTask<>(task);
                future = taskCache.putIfAbsent(taskName, futureTask); // 1.3
                if (future == null) {
                    future = futureTask;
                    futureTask.run(); // 1.4执行任务
                }
            }
            try {
                return future.get(); // 1.5,
            } catch (CancellationException e) {
                taskCache.remove(taskName, future);
            }
        }
    }

    @Test
    public void test7() throws ExecutionException, InterruptedException {
        Demo demo = new Demo();
        String awdf = demo.executionTask("awdf");
        System.out.println(awdf);
    }
    @Test
    public void test6() {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(3);
            System.out.println(i1);
        }
    }
    @Test
    public void test5() {
        Map<String, Object> map = new HashMap<>();
        Object sdfafads = map.putIfAbsent("123", 1);
        System.out.println(sdfafads);
        Object sdfafads1 = map.putIfAbsent("123", 2 );
        System.out.println(sdfafads1);

    }
    @Test
    public void test4() {
//        String s1 = "new String();";
        String s1 = "sadf";
        String s = StringUtil.simpleClassName(s1);
        System.out.println(s);
    }
    @Test
    public void test3() {
        AtomicInteger in = new AtomicInteger();
//        int andIncrement = in.getAndIncrement();
        System.out.println(in.getAndIncrement());
        System.out.println(in.getAndIncrement());
        System.out.println(in.getAndIncrement());
    }
    @Test
    public void test2() {
        SecurityManager securityManager = System.getSecurityManager();
        System.out.println();
    }
    @Test
    public void test1() {
        int i = Runtime.getRuntime().availableProcessors();
        System.out.println(i);
//        Runtime.getRuntime().
    }

}
